import { IBatchAddOrigRouteItem } from './interface'
import type { IStore } from './store'

export const ROW_NUM = 50

export const createEmptyRows = (num: number): IBatchAddOrigRouteItem[] => {
  return Array(num)
    .fill(null)
    .map<IBatchAddOrigRouteItem>(() => ({
      waybillNo: '',
      originalRoute: '',
      occurDate: '',
    }))
}

export const isEmptyRow = (data: IBatchAddOrigRouteItem | null | undefined) => {
  return !data?.waybillNo && !data?.originalRoute && !data?.occurDate
}

export const createInitialData = () => createEmptyRows(ROW_NUM)

export const getRowData = (
  gridRef: IStore['gridRef'],
): {
  /** 所有行 */
  rowData: IBatchAddOrigRouteItem[]
  /** 所有非空行 */
  noEmptyRowData: IBatchAddOrigRouteItem[]
  /** 所有非空行索引 */
  noEmptyRowIndexs: number[]
  /** 所有校验通过行 */
  validRowData: IBatchAddOrigRouteItem[]
  /** 所有校验通过行索引 */
  validRowIndexs: number[]
} => {
  const rowData: IBatchAddOrigRouteItem[] = []
  const noEmptyRowData: IBatchAddOrigRouteItem[] = []
  const noEmptyRowIndexs: number[] = []
  const validRowData: IBatchAddOrigRouteItem[] = []
  const validRowIndexs: number[] = []
  gridRef.current?.api.forEachNode((rowNode, index) => {
    const data = rowNode.data!
    rowData.push(data)
    if (data.waybillNo || data.originalRoute || data.occurDate) {
      noEmptyRowData.push(data)
      noEmptyRowIndexs.push(index)
      if (data.waybillNo && data.originalRoute && data.occurDate && data.resultType !== 2 && data.resultType !== 3) {
        validRowData.push(data)
        validRowIndexs.push(index)
      }
    }
  })
  return { rowData, noEmptyRowData, noEmptyRowIndexs, validRowData, validRowIndexs }
}
